package com.ta.stage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.dao.KeywordDao;
import com.ta.stage.entity.Dgroup;
import com.ta.stage.entity.Keyword;
import com.ta.stage.entity.vo.KeywordVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.KeywordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/keyword/")
@Api(tags={"关键词 KeywordController"})
public class KeywordController {

    @Autowired
    private KeywordService keywordService;

    @ApiOperation(value = "查询关键词")
    @RequestMapping(value = "findKeyword", method = RequestMethod.GET)
    @ResponseBody
    public Result findKeyword() {
        List<Keyword> keywordList = keywordService.list(null);
        return Result.ok(keywordList);
    }

    @ApiOperation(value = "根据id查询关键词")
    @GetMapping("getKeyword/{id}")
    public Result getKeyword(@PathVariable Long id) {
        Keyword keyword = keywordService.getById(id);
        return Result.ok(keyword);
    }

    @ApiOperation(value = "新增关键词")
    @RequestMapping(value = "saveKeyword", method = RequestMethod.POST)
    @ResponseBody
    public Result saveKeyword(@RequestBody Keyword keyword) {
        boolean save = keywordService.save(keyword);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "逻辑删除关键词")
    @DeleteMapping("{id}")
    public Result removeKeyword(@PathVariable Long id) {
        boolean flag = keywordService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改关键词")
    @PostMapping("updateKeyword")
    public Result updateKeyword(@RequestBody Keyword keyword) {
        boolean flag = keywordService.updateById(keyword);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除关键词")
    @DeleteMapping("batchRemoveKeyword")
    public Result batchRemoveKeyword(@RequestBody List<Long> idList) {
        keywordService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "关键词条件查询带分页")
    @PostMapping("findPageKeyword/{current}/{limit}")
    public Result findPageKeyword(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) KeywordVo keywordVo) {

        //创建page对象，传递当前页，每页记录数
        Page<Keyword> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<Keyword> wrapper = new QueryWrapper<>();
        Integer id = keywordVo.getId();
        String name = keywordVo.getName();

        //条件可能为空
        if (!StringUtils.isEmpty(id)) {
            wrapper.eq("id", keywordVo.getId());
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", keywordVo.getName());
        }
            //调用方法实现分页查询
            Page<Keyword> pageKeyword = keywordService.page(page, wrapper);
            return Result.ok(pageKeyword);

        }
}
